package work.coffee.connect;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.swing.JOptionPane;

public class ConnectionFactory {
	private String driver;
	private String url;
	private String user;
	private String password;
	
	private static ConnectionFactory instance = new ConnectionFactory();
	
	public static Connection getConnection(){
		return instance.createConnection();
	}

	
	public ConnectionFactory() {
		loadProperties();
		
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			JOptionPane.showMessageDialog(null, "MySQL Drive not found");
			System.exit(-1);
		}
	}


	private void loadProperties() {
		Properties prop = PropDb.loadProperties("dbcon.xml");
		driver = prop.getProperty("DRIVER");
		url = prop.getProperty("URL");
		user = prop.getProperty("USER");
		password = prop.getProperty("PASSWORD");		
	}


	private Connection createConnection() {
		Connection con = null;
		try {
			con = DriverManager.getConnection(url,user,password);
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "Unable connection to MySQL");
		}
		return con;
	}
}